package twilight.companion.entity.ai;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import twilight.companion.entity.MiniYetiEntity;

import java.util.EnumSet;
import java.util.List;

public class MiniYetiHighPriorityThrowGoal extends Goal {
    private final MiniYetiEntity yeti;
    private final double detectionRange;
    private LivingEntity nearbyTarget;
    private int throwTimer;
    private int cooldownTimer;

    public MiniYetiHighPriorityThrowGoal(MiniYetiEntity yeti, double detectionRange) {
        this.yeti = yeti;
        this.detectionRange = detectionRange;
        this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK, Goal.Flag.TARGET));
    }

    @Override
    public boolean canUse() {
        if (this.cooldownTimer > 0) {
            this.cooldownTimer--;
            return false;
        }

        if (!this.yeti.getPassengers().isEmpty()) {
            return false;
        }

        this.nearbyTarget = this.findNearbyTarget();
        return this.nearbyTarget != null;
    }

    @Override
    public boolean canContinueToUse() {
        if (!this.yeti.getPassengers().isEmpty()) {
            return this.throwTimer > 0;
        }

        if (this.nearbyTarget == null || !this.nearbyTarget.isAlive()) {
            return false;
        }

        double distance = this.yeti.distanceToSqr(this.nearbyTarget);
        return distance <= this.detectionRange * this.detectionRange;
    }

    @Override
    public void start() {
        this.throwTimer = 40;
        if (this.nearbyTarget != null) {
            this.yeti.setTarget(this.nearbyTarget);
        }
    }

    @Override
    public void stop() {
        if (!this.yeti.getPassengers().isEmpty()) {
            Entity rider = this.yeti.getPassengers().get(0);
            rider.stopRiding();

            Vec3 throwDirection;
            if (this.nearbyTarget != null && this.nearbyTarget.isAlive()) {
                Vec3 targetPos = this.nearbyTarget.position();
                Vec3 yetiPos = this.yeti.position();
                throwDirection = targetPos.subtract(yetiPos).normalize();
            } else {
                throwDirection = this.yeti.getLookAngle();
            }

            Vec3 throwVec = new Vec3(throwDirection.x * 2.0, 0.8, throwDirection.z * 2.0);
            rider.push(throwVec.x(), throwVec.y(), throwVec.z());
        }

        this.nearbyTarget = null;
        this.cooldownTimer = 100;
    }

    @Override
    public void tick() {
        if (!this.yeti.getPassengers().isEmpty()) {
            this.throwTimer--;
            if (this.nearbyTarget != null) {
                this.yeti.getLookControl().setLookAt(this.nearbyTarget, 30.0F, 30.0F);
            }
        } else if (this.nearbyTarget != null) {
            double distance = this.yeti.distanceToSqr(this.nearbyTarget);
            
            if (distance <= 4.0) {
                this.nearbyTarget.stopRiding();
                this.nearbyTarget.startRiding(this.yeti, true);
                this.yeti.swing(InteractionHand.MAIN_HAND);
            } else {
                this.yeti.getNavigation().moveTo(this.nearbyTarget, 1.2);
                this.yeti.getLookControl().setLookAt(this.nearbyTarget, 30.0F, 30.0F);
            }
        }
    }

    private LivingEntity findNearbyTarget() {
        AABB searchArea = this.yeti.getBoundingBox().inflate(this.detectionRange);
        List<LivingEntity> nearbyEntities = this.yeti.level().getEntitiesOfClass(LivingEntity.class, searchArea);

        for (LivingEntity entity : nearbyEntities) {
            if (entity == this.yeti) {
                continue;
            }

            if (this.yeti.isTame() && entity == this.yeti.getOwner()) {
                continue;
            }

            if (this.yeti.isTame() && entity instanceof MiniYetiEntity otherYeti && 
                otherYeti.isTame() && otherYeti.getOwner() == this.yeti.getOwner()) {
                continue;
            }

            if (this.yeti.canAttack(entity)) {
                return entity;
            }
        }
        return null;
    }
}